﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class TrackingEListDAO : BaseDAO
    {
        #region Backend method
        /// <summary>
        /// Insert TrackingEList
        /// </summary>
        /// <param name="trackingelist"></param>
        public void Create(TrackingEList trackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListCreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingE", trackingelist.ID_TrackingE));
                command.Parameters.Add(new SqlParameter("@Date", trackingelist.Date));
                command.Parameters.Add(new SqlParameter("@PostUser", trackingelist.PostUser));
                command.Parameters.Add(new SqlParameter("@TrackEvent_vi", trackingelist.TrackEvent_vi));
                command.Parameters.Add(new SqlParameter("@TrackEvent_en", trackingelist.TrackEvent_en));
                command.Parameters.Add(new SqlParameter("@State_vi", trackingelist.State_vi));
                command.Parameters.Add(new SqlParameter("@State_en", trackingelist.State_en));
                
                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                //TrackingEList.ID = (long)paramId.Value;
                //else
                //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, ""));
            }
        }

        /// <summary>
        /// Edit TrackingEList 
        /// </summary>
        /// <param name=""></param>
        public void Edit(TrackingEList trackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingEList", trackingelist.ID_TrackingEList));
                command.Parameters.Add(new SqlParameter("@ID_TrackingE", trackingelist.ID_TrackingE));
                command.Parameters.Add(new SqlParameter("@Date", trackingelist.Date));
                command.Parameters.Add(new SqlParameter("@EditUser", trackingelist.EditUser));
                command.Parameters.Add(new SqlParameter("@TrackEvent_vi", trackingelist.TrackEvent_vi));
                command.Parameters.Add(new SqlParameter("@TrackEvent_en", trackingelist.TrackEvent_en));
                command.Parameters.Add(new SqlParameter("@State_vi", trackingelist.State_vi));
                command.Parameters.Add(new SqlParameter("@State_en", trackingelist.State_en));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "", .ID));
            }
        }

        /// <summary>
        /// On/Off view
        /// </summary>
        /// <param name="trackingelist"></param>
        public void OnOff(TrackingEList trackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListOnOff", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingEList", trackingelist.ID_TrackingEList));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "", .ID));
            }
        }


        /// <summary>
        /// Delete TrackingEList 
        /// </summary>
        /// <param name=""></param>
        public void Delete(TrackingEList trackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingEList", trackingelist.ID_TrackingEList));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "", .ID));
            }
        }

        /// <summary>
        /// Show TrackingEList  All
        /// </summary>
        /// <returns></returns>
        public DataTable TrackingEListList(String language)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListList", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show TrackingEList All
        /// </summary>
        /// <returns></returns>
        public DataTable TrackingEListListById(int idtrackinge, String language)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListListById", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@ID_TrackingE", SqlDbType.Int).Value = idtrackinge;
                command.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable TrackingEListList_GetByID(int idtrackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_TrackingEList", SqlDbType.Int).Value = idtrackingelist;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idtrackingelist"></param>
        /// <returns></returns>
        public TrackingEList GetTrackingEListByID(int idtrackingelist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEListEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingEList", idtrackingelist));

                connection.Open();
                TrackingEList trackingelist = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        trackingelist = CreateTrackingEListFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "", ID));
                }

                return trackingelist;
            }
        }

        private TrackingEList CreateTrackingEListFromReader(IDataReader reader)
        {
            TrackingEList trackingelist = new TrackingEList();
            trackingelist.ID_TrackingEList = (int)reader["ID_TrackingEList"];
            trackingelist.ID_TrackingE = (int)reader["ID_TrackingE"];
            trackingelist.TrackEvent = (string)reader["TrackEvent"];
            trackingelist.Date = (DateTime)reader["Date"];
            trackingelist.State = (string)reader["State"];
            trackingelist.Visible = (Boolean)reader["Visible"];
            trackingelist.Language = (string)reader["Language"];
            return trackingelist;
        }
        #endregion
    }
}